import 'dart:math';
import 'level_models.dart';

class QuestionGenerator {
  final Random _random = Random();

  Map<String, dynamic> generateQuestion(String category, int difficulty) {
    final numberRange = _getNumberRange(difficulty);
    
    switch (category) {
      case '加法':
        return _generateAddition(numberRange);
      case '减法':
        return _generateSubtraction(numberRange);
      case '乘法':
        return _generateMultiplication(numberRange);
      case '除法':
        return _generateDivision(numberRange);
      default:
        return _generateMixed(numberRange);
    }
  }

  NumberRange _getNumberRange(int difficulty) {
    switch (difficulty) {
      case 1:
        return const NumberRange(0, 10);
      case 2:
        return const NumberRange(0, 50);
      case 3:
        return const NumberRange(0, 100);
      default:
        return const NumberRange(0, 10);
    }
  }

  Map<String, dynamic> _generateAddition(NumberRange range) {
    final num1 = _random.nextInt(range.max - range.min) + range.min;
    final num2 = _random.nextInt(range.max - range.min) + range.min;
    return {
      'question': '$num1 + $num2',
      'answer': num1 + num2,
      'options': _generateOptions(num1 + num2, range)
    };
  }

  Map<String, dynamic> _generateSubtraction(NumberRange range) {
    final num1 = _random.nextInt(range.max - range.min) + range.min;
    final num2 = _random.nextInt(num1 - range.min) + range.min;
    return {
      'question': '$num1 - $num2',
      'answer': num1 - num2,
      'options': _generateOptions(num1 - num2, range)
    };
  }

  Map<String, dynamic> _generateMultiplication(NumberRange range) {
    final num1 = _random.nextInt((range.max ~/ 2) - range.min) + range.min;
    final num2 = _random.nextInt((range.max ~/ 2) - range.min) + range.min;
    return {
      'question': '$num1 × $num2',
      'answer': num1 * num2,
      'options': _generateOptions(num1 * num2, range)
    };
  }

  Map<String, dynamic> _generateDivision(NumberRange range) {
    final num2 = _random.nextInt((range.max ~/ 2) - range.min) + range.min + 1;
    final result = _random.nextInt((range.max ~/ 2) - range.min) + range.min;
    final num1 = num2 * result;
    return {
      'question': '$num1 ÷ $num2',
      'answer': result,
      'options': _generateOptions(result, range)
    };
  }

  Map<String, dynamic> _generateMixed(NumberRange range) {
    final operations = ['+', '-', '×', '÷'];
    final opIndex = _random.nextInt(operations.length);
    switch (operations[opIndex]) {
      case '+':
        return _generateAddition(range);
      case '-':
        return _generateSubtraction(range);
      case '×':
        return _generateMultiplication(range);
      case '÷':
        return _generateDivision(range);
      default:
        return _generateAddition(range);
    }
  }

  List<int> _generateOptions(int correctAnswer, NumberRange range) {
    final options = <int>{};
    options.add(correctAnswer);

    while (options.length < 4) {
      // 添加相关干扰项（±1-3）
      var diff = _random.nextInt(3) + 1;
      if (_random.nextBool()) {
        diff = -diff;
      }
      final relatedOption = correctAnswer + diff;
      
      // 添加随机干扰项（0-20）
      final randomOption = _random.nextInt(20);
      
      options.add(relatedOption);
      options.add(randomOption);
    }
    
    return options.toList()..shuffle();
  }
}